using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Promike.site.Models;

namespace Promike.site.Controllers
{
    public class PostController : Controller
    {
        private readonly IPostRepository postRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public PostController()
            : this(new PostRepository())
        {
        }

        public PostController(IPostRepository postRepository)
        {
            this.postRepository = postRepository;
        }

        //
        // GET: /Post/
        [Authorize(Users = "Admin")]
        public ViewResult Index()
        {
            return View(postRepository.All);
        }

        public ActionResult All()
        {
            return View(postRepository.All.Where(post => post.Visible).OrderByDescending(o => o.ID));
        }

        public ActionResult Search(int id)
        {
            return View("All", postRepository.All.Where(post => post.Visible).Where(x => x.TagID == id).OrderByDescending(o => o.ID));
        }

        public ActionResult SearchText(string searchText)
        {
            return View("SearchResults", postRepository.AllIncluding().Where(post => post.Visible).Where(x => x.Title.Contains(searchText) || x.MainContent.Contains(searchText)).OrderByDescending(o => o.ID));
        }

        public ActionResult Download(string fileName)
        {
            var path = Server.MapPath(@"\Content\files\") + fileName;
            return File(path, "application/octet-stream", fileName);
        }

        public ViewResult Details(int id)
        {
            return View(postRepository.Find(id));
        }

        //
        // GET: /Post/Create
        [Authorize(Users = "Admin")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Post/Create
        [Authorize(Users = "Admin")]
        [HttpPost]
        public ActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                postRepository.InsertOrUpdate(post);
                postRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Post/Edit/5
        [Authorize(Users = "Admin")]
        public ActionResult Edit(int id)
        {
            return View(postRepository.Find(id));
        }

        //
        // POST: /Post/Edit/5
        [Authorize(Users = "Admin")]
        [HttpPost]
        public ActionResult Edit(Post post)
        {
            if (ModelState.IsValid)
            {
                postRepository.InsertOrUpdate(post);
                postRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Post/Delete/5
        [Authorize(Users = "Admin")]
        public ActionResult Delete(int id)
        {
            return View(postRepository.Find(id));
        }

        //
        // POST: /Post/Delete/5
        [Authorize(Users = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            postRepository.Delete(id);
            postRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                postRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

